package com.bocai.yuweiwangluo.activity;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.adapter.GuideVpAdapter;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import java.util.ArrayList;
import java.util.List;



/**
 * 新手引导界面
 */
public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener
{
    @ViewInject(R.id.guide_vp)
    ViewPager mViewPager;
    @ViewInject(R.id.guide_llayout)
    LinearLayout mLayout;
    @ViewInject(R.id.tv_tologin)
    TextView tvToLogin;
    private View mView;
    private View mDotView;
    private GuideVpAdapter mVpAdapter;
    private int mOldPosition;
    private int preState;
    private int[] img = {R.mipmap.guide_one, R.mipmap.guide_two, R.mipmap.guide_three};
    private List<View> mList = new ArrayList<>();
    private  int what;

    @Override
    protected int contentView()
    {
        return R.layout.activity_guide;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        what=getIntent().getIntExtra("what",1);
        initView();
    }

    private void initView()
    {
        for (int i = 0; i < img.length; i++)
        {
            mView = new View(this);
            mDotView = new View(this);
             mDotView.setBackgroundResource(R.drawable.guide_dot_unchecked);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
            if (i > 0)
            {
                params.leftMargin = 15;// 设置圆点间隔
            }
            mDotView.setLayoutParams(params);// 设置圆点的大小
            mLayout.addView(mDotView);// 将圆点添加给线性布局
            mView.setBackgroundResource(img[i]);
            mList.add(mView);
        }
        //mLlayout.getChildAt(mOldPosition).setBackgroundResource(R.drawable.guide_dot_checked);//设置第一个点为选中
        mVpAdapter = new GuideVpAdapter(mList);
        mViewPager.setAdapter(mVpAdapter);
        //viewpager滑动事件
        mViewPager.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
    {
    }

    @Override
    public void onPageSelected(int position)
    {
        mLayout.getChildAt(mOldPosition).setBackgroundResource(R.drawable.guide_dot_unchecked);
        mLayout.getChildAt(position).setBackgroundResource(R.drawable.guide_dot_checked);
        mOldPosition = position;
        if(position==img.length-1){
            tvToLogin.setVisibility(View.VISIBLE);
            tvToLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(what==1){

                        Intent intent = new Intent(GuideActivity.this,LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }else{
                        finish();
                    }
                }
            });
        }
    }

    @Override
    public void onPageScrollStateChanged(int state)
    {
        if(preState == 1 && state == 0 && mOldPosition == img.length - 1)
        {
            if(what==1){

                Intent intent = new Intent(GuideActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }else{
                finish();
            }
        }
        preState = state;
    }
}
